
/**
 * name:        个人中心--售后服务步骤二
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   chinadangmai.com
 */

$(function(){
    // 个人中心首页 
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var token='' 
        isLogin();    
        getToken();     // 获取/验证token 
        if( token!='' && token!=null){
            var refundId =GetQueryString("refundId"); 
            var goodsId =GetQueryString("goodsId"); 
            getRefundOrderDetail(token,URL,refundId,goodsId);
        }
    }else{
        window.location.href='/src/login/login.html?type=2'
    }

    // 导航栏显示已登录
    function isLogin(){
        $('.yes_or_no').remove();
        $('.mod_login').addClass('mod_login2')
        var nav=''
        nav+='<span class="hello" style="margin-left:50px;">您好，</span><span class="nike_name"></span>'
        // nav+='<span class="avater"><img src="../images/base/member_msg_pic.png" alt=""></span>'
        nav+='<i class="icon_down"></i>'
        nav+='<ul class="logined">'
        nav+='<li class="member_center"><a href="../member/member_index.html"><i></i><span>会员中心</span></a></li>'
        nav+='<li class="login_out"><a href="javascript:;"><i></i><span>退出登录</span></a></li>'
        nav+='</ul>'
        $('.mod_login').append(nav);
    }

    // 获取/验证token
    function getToken(){
        strs=$.cookie('user').split(",");
        for(var i in strs){ 
            $('.nike_name').html(strs[1]) 
            token=strs[6];
            name=strs[1];
        }
    }
    // 退出登录
    $('.login_out').click(function(){
        $.cookie('user', null, { path: '/' });
            $.ajax({
                type:'post',
                url:new_url+'shop/login/logOut',
                dataType:'json',
                data:'',
                success:function(data){
                    tc(data.message)
                }  
            })
            setTimeout(function(){
                window.location.href='/index.html'
            },1100);
    });

    

});

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  decodeURI(r[2]); return null;
}

function getRefundOrderDetail(token,URL,refundId,goodsId){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_return/getRefundOrderDetail',
        dataType:'json',
        data:{token:token,refundId:refundId,goodsId:goodsId},
        success:function(e){ 
            if(e.data.orderInfo.spec=='' || e.data.orderInfo.spec==null){
                e.data.orderInfo.spec='默认';
            }
            var serviceGoodsTpl = $("#serviceGoodsTpl").html();
            var time = e.data.orderInfo.createTime;
            var refundState = e.data.orderInfo.refundState;
            function getLocalTime(nS) {     
                return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, " ").replace(/日/g, " ");  
            }
            // 售后状态 1.待审核 2.审核不通过 3.审核通过 4.等待买家发货 5.买家已发货 6.卖家未收到货 7.已完成 8.退款关闭
            if(refundState == 1){
                refundState = '待审核';
            }
            if(refundState == 2){
                refundState = '审核不通过';
                $('.service_step_tit').remove();
            }
            if(refundState == 3){
                refundState = '审核通过';
            }
            if(refundState == 4){
                refundState = '等待买家发货';
            }
            if(refundState == 5){
                refundState = '买家已发货';
            }
            if(refundState == 6){
                refundState = '卖家未收到货';
            }
            if(refundState == 7){
                refundState = '卖家已收货';
            }
			if(refundState == 8){
                refundState = '退款已完成';
            }
			if(refundState == 9){
                refundState = '退款关闭';
            }
            // 商品信息
            var serviceGoodsCont=template(
                serviceGoodsTpl, {
                    serviceGoodsList: [{
                        createTime:getLocalTime(time),
                        goodsImg:e.data.orderInfo.image,
                        goodsName:e.data.orderInfo.name,
                        goodsSpec:e.data.orderInfo.spec,
                        orderNo:e.data.orderInfo.orderNo,
                        payPrice:e.data.orderInfo.payPrice,
                        refundAmount:e.data.orderInfo.refundAmount,
                        refundId:e.data.orderInfo.refundId,
                        refundNo:e.data.orderInfo.refundNo,
                        refundNum:e.data.orderInfo.refundNum,
                        refundState:refundState,
                    }]
                }
            );
            $('.service_goods').append(serviceGoodsCont); 
            if(e.data.orderInfo.refundType != '1'){
                // 判断显示模板 
                if(e.data.logisticsInfo.length != '0'){
                    // 物流模板
                    var logisticsTpl = $("#logisticsTpl").html();
                    var logisticsCont=template(
                        logisticsTpl, {
                            logisticsList: [{
                                logisticsName:e.data.logisticsInfo.name,
                                logisticsNo:e.data.logisticsInfo.logisticsNo,
                            }]
                        }
                    );
                    $('.service_wuliu').append(logisticsCont); 
                    $(".logisticsInfo").css('height','60px');
                    var logisticsInfoTpl = $("#logisticsInfoTpl").html();
                    for(var i=0; i < e.data.logisticsInfo.logList.length; i++){
                        var logisticsInfoCont=template(
                            logisticsInfoTpl, {
                                logisticsInfoList: [{
                                    time:e.data.logisticsInfo.logList[i].time,
                                    context: e.data.logisticsInfo.logList[i].context,
                                }]
                            }
                        );
                        $('.logisticsInfo').append(logisticsInfoCont);
                    }
                    // 查看更多物流信息
                    var isAuto = true;
                    $(".more_view").click(function(){
                        if(isAuto){
                            $(".logisticsInfo").css('height','auto');
                            $(this).text('收起物流');
                            isAuto = false;
                        }else{
                            $(".logisticsInfo").css('height','60px');
                            $(this).text('查看更多');
                            isAuto = true;
                        }
                    });
                }else{
                    // $(".service_view_detail").find('a').text('等待买家发货');
                    // 添加物流公司
                    var logisticsTpl = '';
                    logisticsTpl += '<div class="service_wuliu_tit">';
                    // logisticsTpl += '<span>剩余<label style="color:#f10213;">13天02小时47分21秒</label>，逾期未填写退货物流信息，则退货将自动关闭,并且无法继续申请。</span>';
                    logisticsTpl + '<span>添加物流</span>';
                    logisticsTpl += '</div>';
                    logisticsTpl += '<div class="service_wuliu_cont">';
                    logisticsTpl += '<p>退货地址：福建省厦门市湖里区殿前街道闽南古镇A座8074卓越青凡当买收</p>';
                    logisticsTpl += '<a href="javascript:;" class="add_logistics">填写物流信息</a>';
                    logisticsTpl += '</div>';
                    $('.service_wuliu').append(logisticsTpl);
                    $(".add_logistics").click(function(){
                        logisticsTc();
                        $('.popup_wrap').css({
                            'margin-top':'-225px'
                        });
                        getLogisticsCompanyList(token,URL);
                        $(".logistics_btn").click(function(){
                            var phone = $('#phone').val();
                            var companyId = $("#companyId").val();
                            var shippingNo = $("#shippingNo").val();
                            var description = $("#description").val(); 
                            if(companyId == ''){
                                tc('请选择快递公司');
                                return false; 
                            }
                            if(shippingNo == ''){
                                tc('请输入快递单号');
                                return false; 
                            }
                            if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
                                tc("请输入正确的手机号")
                                return false;    
                            }
                            if(description == ''){
                                tc('请填写发货说明');
                                return false; 
                            }
                            var data = {
                                token:token,
                                companyId:companyId,
                                shippingNo:shippingNo,
                                phone:phone,
                                description:description,
                                refundId:refundId,
                            }
                            $.ajax({
                                type:'post',
                                url:new_url+'shop/member_return/addRefundLogisticsInfo',
                                dataType:'json',
                                data:data,
                                success:function(s){ 
                                    if(s.code=='0'){
                                        tc('添加成功');
                                        setTimeout(function(){
                                            window.location.reload();
                                        },1000);
                                    }else{
                                        tc(s.message);
                                    }
                                }
                            })
                        });
                    });
                    
                }  
            }else{ 
                if(e.data.orderInfo.refundState >= 3){ 
                    $('.service_step_tit ul li').removeClass('active').addClass('active_01').last().removeClass('active_01').addClass('active')
                }
                $('.service_wuliu').remove();
            }
            

            // 售后处理日志
            if(e.data.handleLog.length != 0){
                var handleLogTpl = $("#handleLogTpl").html();
                var handleLogCont=template(
                    handleLogTpl, {
                        handleLogList: [{
                            logisticsName:e.data.handleLog.name,
                            logisticsNo:e.data.handleLog.logisticsNo,
                        }]
                    }
                );
                $('.service_wuliu').append(handleLogCont);
            }else{
                $(".service_record_cont").append('<div style="width:100%;height:100px;line-height:100px;text-align:center;">暂无售后处理消息</div>')
            }
            
        }
    });
}

function logisticsTc(){
    $(".popup_bg").css('display','block');
    var t ='';
    t += '<div class="popup_wrap">';
    t += '<div class="popup">';
    t += '<div class="popup_title">';
    t += '<span class="title_txt">填写退货物流信息</span>';
    t += '<span class="close"></span>';
    t += '</div>';
    t += '<div class="popup_content" style="width:320px;margin:0 auto;">';
    t += '<div class="logistics_box">';
    t += '<span>物流公司</span>';
    t += '<select name="" id="companyId" class="logistics_company">';
    t += '<option value="">请选择快递公司</option>';
    t += '</select>';
    t += '</div>';
    t += '<div class="logistics_box">';
    t += '<span>快递单号</span>';
    t += '<div class="border_box"></div>';
    t += '<input type="text" class="shipping_no" id="shippingNo" maxlength="20">';
    t += '</div>';
    t += '<div class="logistics_box">';
    t += '<span>联系人电话</span>';
    t += '<div class="border_box"></div>';
    t += '<input type="text" class="phone" id="phone">';
    t += '</div>';
    t += '<div class="logistics_box" style="height:95px;">';
    t += '<span style="height:95px;line-height:95px;">发货说明</span>';
    t += '<textarea name="" id="description"></textarea>';
    t += '</div>';
    t += '<a href="javascript:;" class="logistics_btn">提交</a>';
    t += '</div>';
    t += '</div>';
    t += '</div>';
    $('body').append(t);
    close_popup();
}
// 关闭弹窗
function close_popup(){
    $(".popup_bg, .close, .cancel").click(function(){
        $(".popup_bg").css({"display":"none"});
        $(".popup_wrap").remove();
    });
}

function getLogisticsCompanyList(token,URL){
    $.ajax({
        type:'post',
        url:new_url+'shop/member_return/getLogisticsCompanyList',
        dataType:'json',
        data:{token:token},
        success:function(e){ 
            if(e.code == '0'){
                for(var i=0; i < e.data.length; i++){
                    var t='<option value="'+e.data[i].companyId+'">'+e.data[i].name+'</option>';
                    $("#companyId").append(t);
                }
            }else{
                tc(e.message)
            }
        }
    });
}